﻿
Imports System.Data.SqlClient
Public Class ComentarioDAO
    Public Function insertar(ByVal modelo As Comentario) As Comentario
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO Comentario (idInmueble,idEstudiante,Contenido) VALUES (@VALOR2, @VALOR3, @VALOR4)"

            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR2", modelo.idInmueble)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.idEstudiante)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.Contenido)

            Dim afectados = cmd.ExecuteNonQuery
            If afectados <> 1 Then
                Throw New DAOExcepcion("ComentarioDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            cmd = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idEstudiante = CType(dr(0), Integer)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try
        Return modelo

    End Function

    Public Function obtener(ByVal idComentario As Integer) As Comentario
        Dim modelo As New Comentario

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT idComentario,Contenido FROM Comentario WHERE idComentario=@VALOR1"
            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", idComentario)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idComentario = dr(0).ToString
                modelo.Contenido = dr(1).ToString

            End If

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function


    Public Function listar() As Collection
        Dim lista As New Collection
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT idComentario,Contenido FROM Comentario"
            Dim cmd As New SqlCommand(sql, cn)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While dr.Read
                Dim modelo As New Comentario

                modelo.idComentario = dr(0).ToString
                modelo.Contenido = dr(1).ToString

                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function


    Public Sub eliminar(ByVal idComentario As Integer)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "DELETE FROM Comentario WHERE idComentario=@VALOR1"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", idComentario)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("ComentarioDAO: No eliminó")
            End If

            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub
End Class


